package com.united.ucts.modules.juc.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * JUC并发编程学习笔记样例代码
 *
 * @author hudan
 * @version 1.0
 * @filename ThreadDaemonDemo.java
 * @copyright <a href="www.chancein.com">www.chancein.com</a>
 * @date 2022-09-15
 */
@Slf4j
public class ThreadDaemonDemo {

    public static void main(String[] args) {
        // 一般情况下不做特殊说明配置,用户新建的线程都是用户线程
        Thread thread = new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            System.out.println(Thread.currentThread().getName() + "\t 开始运行, " +
                    (Thread.currentThread().isDaemon() ? "守护线程" : "用户线程"));
            while (true) {

            }
        }, "this threa name: " + Thread.currentThread().getName() + "will start!");
        thread.start();

        /* 将当前线程暂停休眠几秒钟(默认3秒) */
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("休眠当前线程:[{}]出现异常:[{}]", Thread.currentThread().getName(), e.getMessage());
        }
    }
}